package com.glorysoft.wangnianxinpda.activity;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentManager;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.glorysoft.wangnianxinpda.R;
import com.glorysoft.wangnianxinpda.base.App;
import com.glorysoft.wangnianxinpda.base.BaseActivity;
import com.glorysoft.wangnianxinpda.base.Constant;
import com.glorysoft.wangnianxinpda.databinding.ActivityMaterialExchangeScanBinding;
import com.glorysoft.wangnianxinpda.entity.ChangeMaterialInfoEntity;
import com.glorysoft.wangnianxinpda.entity.MaterialInfoEntity;
import com.glorysoft.wangnianxinpda.entity.PDACurrentMtReturnEntity;
import com.glorysoft.wangnianxinpda.entity.net.ResponseEntity;
import com.glorysoft.wangnianxinpda.utils.GsonUtils;
import com.glorysoft.wangnianxinpda.utils.MyUtils;
import com.glorysoft.wangnianxinpda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_TextCallback;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

import java.io.Serializable;
import java.util.HashMap;

public class MaterialExchangeScanActivity extends BaseActivity {
    private Context mContext;

    private String sEqpID = "";//设备号
    private String sSn = "";//物料条码
    private String sLotId = "";//工单批次号
    private String sMatLotId = "";//物料批次号


    private MaterialInfoEntity mMaterialInfoEntity;//物料信息
    private String sOldMaterialCode = "";//旧物料编号
    private FreeUI_GeneralFragmentDialog mMessageDialog;//消息提示框
    private String sMaterialType = "";//物料类型
    private String sMatLot = "";//物料类型
    private String sUserQTY = "";//物料类型
    private ActivityMaterialExchangeScanBinding binding = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutResId() {
        return 0;
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_material_exchange_scan);

        mContext = this;

        binding.etSn.requestFocus();

    }

    @Override
    protected void initData() {
        sEqpID = getIntent().getStringExtra("EQPID");
        sOldMaterialCode =  getIntent().getStringExtra("MATERIALCODE");
        sMaterialType =  getIntent().getStringExtra("MATERIALTYPE");
        sMatLot =  getIntent().getStringExtra("MATLOT");
        sUserQTY =  getIntent().getStringExtra("USERQTY");
        binding.tvCurrentEqpNo.setText(sEqpID);
        MyUtils.setNewEditTextListener(binding.etSn, mContext, s -> {
            sSn = s;
        });
        MyUtils.setNewEditTextListener(binding.etLotId, mContext, s -> {
            sLotId = s;
        });
//        MyUtils.setNewEditTextListener(binding.etMatLotId, mContext, s -> {
//            sMatLotId = s;
//        });

        MyUtils.setEditTextListener(binding.etMatLotId, this, new FreeUI_TextCallback() {
            @Override
            public void onSuccess(String s) {
                FreeUI_EntityCallBack<ResponseEntity<PDACurrentMtReturnEntity>> callBack = new FreeUI_DialogEntityCallBack<ResponseEntity<PDACurrentMtReturnEntity>>(new TypeToken<ResponseEntity<PDACurrentMtReturnEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {
                    @Override
                    public void onSuccess(Response<ResponseEntity<PDACurrentMtReturnEntity>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(false, getSupportFragmentManager())) {
                            binding.etQTY.setText(response.body().getData().getUSERQTY());
                        }
                    }

                    @Override
                    public void onError(Response<ResponseEntity<PDACurrentMtReturnEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(), "PDAGetMatInfo");
                    }
                };

                JsonObject map = new JsonObject();
                map.addProperty("WORK_NO", Constant.loginEntity.getWORK_NO());//工号
                map.addProperty("EQPID", sEqpID);//设备号
                map.addProperty("MLOTID", s);//物料批号
                String method = "PDAGetMatInfo";
                NetUtils.requestPostNet("PDAGetMatInfoRequest", method, this, map, callBack);
            }
        });
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle(getString(R.string.materialReplacement));
        freeUI_titleView.setRightListener(getString(R.string.replace), v -> {
            if (sSn.equals("")){
                showToast(getString(R.string.please_scan_material_sn), false);
                return;
            }
//            if (sLotId.equals("")){
//                showToast(getString(R.string.please_scan_lot), false);
//                return;
//            }
                ChangeMaterialInfo();//更换物料

        });
        return new FreeUI_AddViewEntity("header", binding.llMain);
    }

    //region 接口调用

    //更换物料
    private void ChangeMaterialInfo() {
        FreeUI_EntityCallBack<ResponseEntity<?>> callBack = new FreeUI_DialogEntityCallBack<ResponseEntity<?>>(new TypeToken<ResponseEntity<?>>() {
        }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseEntity<?>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(false, getSupportFragmentManager())) {
                    if (!TextUtils.isEmpty(response.body().getMsg())) {
                        finish();
//                        showMessageDialog(response.body().getMsg(), getSupportFragmentManager());
                    }
                }

            }

            @Override
            public void onError(Response<ResponseEntity<?>> response) {
                super.onError(response);
                loadError(response.getException(), "PDAMATCHANGE");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("WORK_NO", Constant.loginEntity.getWORK_NO());//工号
        map.addProperty("EQP_ID", sEqpID);//设备号
        map.addProperty("MATID", sOldMaterialCode);//物料编号（旧物料）
        map.addProperty("ONMATID", sSn);//物料条码（上机）
        map.addProperty("ONMATLOT", binding.etMatLotId.getText().toString().trim());//物料（领料）批号（上机）
//        map.addProperty("LOTID", sLotId);//工单批号(上机)
        map.addProperty("MATERIALTYPE", sMaterialType);//实物类型
        map.addProperty("MATLOT", sMatLot);//实物类型
        map.addProperty("USERQTY", binding.etQTY.getText().toString().trim());//实物类型
        map.addProperty("DATE", binding.etTime.getText().toString().trim());
        map.addProperty("OUTDATE", binding.etEndTime.getText().toString().trim());//实物类型

        String method = "PDAMATCHANGE";
        NetUtils.requestPostNet("", method, this, map, callBack);
    }

    //endregion

    //消息提示框
    private void showMessageDialog(String message, FragmentManager fragmentManager) {
        FreeApi_DialogUtils.getConfirmDialog(
                App.app, "确定", message, this::finish
        ).show(fragmentManager, "DIALOG");

    }
    //消息提示框
    private void showMessageDialog(String message) {
        mMessageDialog = new FreeUI_GeneralFragmentDialog();
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_prompt, null);
        Button btnConfirm = view.findViewById(R.id.btnConfirm);
        TextView tvTips = view.findViewById(R.id.tvTips);
        btnConfirm.setOnClickListener(v -> mMessageDialog.dismiss());
        tvTips.setText(message);
        mMessageDialog.setLayout(view);
//        mDieTypeDialog.getDialog().setCanceledOnTouchOutside(false);
        mMessageDialog.show(getSupportFragmentManager(), "ShowMessage");
        mMessageDialog.setCallBack(() -> mMessageDialog = null);

    }

    @Override
    protected void onStart() {
        super.onStart();
    }

}